//
//  FPSettleDetailCell.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/2/27.
//

#import "FPSettleDetailCell.h"

#define NormalColor [UIColor hexColor:@"#6A6A6A"]
#define WeightColor [UIColor hexColor:@"#3A3A3A"]

@implementation FPSettleDetailCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    self.contentView.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    self.backView.clipsToBounds = YES;
    self.backView.layer.cornerRadius = 8.f;
    self.titleLab.textColor = [UIColor hexColor:@"#000000"];
    
    self.contentLab.textColor = NormalColor;
    self.companyLab.textColor = NormalColor;
    self.timeLab.textColor = NormalColor;
    
    self.cachetImgView.hidden = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)refreshUIWithModel:(FPSettleModel *)model {
    
    self.cachetImgView.hidden = NO;
    
    self.companyLab.text = @"贷款机构名称：广东宝坤网络小额贷款有限公司";
    
    NSString *proofDateIssuance = [FPLoanDetailModel  timeOfParam:model.proofDateIssuance];;
    self.timeLab.text = [NSString stringWithFormat:@"证明出具日期：%@",proofDateIssuance];
    
    // 申请时间
    NSString *userApplyTime = [FPLoanDetailModel  timeOfParam:model.createTime];
    // 放款日期
    NSString *lendersTime = [FPLoanDetailModel  timeOfParam:model.lendersTime];
    // 审核通过日期
    NSString *auditPassageTime = [FPLoanDetailModel  timeOfParam:model.auditPassageTime];
    // 最后还款日期 最后一期的规定日期
    NSString *lastRepaymentDate = [FPLoanDetailModel  timeOfParam:model.lastRepaymentDate];
    
    NSString *name              = [NSString stringWithFormat:@"【 %@ 】" , FPUserInfoModel.sharedUserInfo.idcardName];
    NSString *idCardNumber      = [NSString stringWithFormat:@"【 %@ 】" , FPUserInfoModel.sharedUserInfo.idcardNumber];
    NSString *applyTime         = [NSString stringWithFormat:@"【 %@ 】" , userApplyTime];
    NSString *contractNumber    = [NSString stringWithFormat:@"【 %@ 】" , model.contractNumber]; // 合同号
    NSString *amount            = [NSString stringWithFormat:@"【 小写：%@ 元" , model.amount];
    NSString *amountCN          = [NSString stringWithFormat:@" 大写：%@ 】" , [NSString toCapitalLetters:model.amount]];
    NSString *startTime         = [NSString stringWithFormat:@"【 %@ 】" , lendersTime];
    NSString *endTime           = [NSString stringWithFormat:@"【 %@ 】" , lastRepaymentDate];
    NSString *lastRepayTime     = [NSString stringWithFormat:@"【 %@ 】" , lastRepaymentDate]; // 截止日期
    
    NSString *text1 = [NSString stringWithFormat:@" %@ ，身份证号为：", name];
    NSString *text2 = [NSString stringWithFormat:@" %@ 于 %@ 向广东宝坤网络小额贷款有限公司申请了一笔贷款，贷款合同编号为 %@ ，贷款金额为人民币 %@，%@ ，贷款期限自 %@ 至 %@ 。" , idCardNumber , applyTime , contractNumber , amount , amountCN , startTime , endTime];
    NSString *text3 = [NSString stringWithFormat:@"截至 %@ ，借款人已按照贷款合同约定，足额偿还了该笔贷款的全部本金、利息及相关费用，贷款已全部结清。", lastRepayTime];
    NSString *text4 = @"经核实，上述贷款已全额结清，借款人不再对该笔贷款承担任何还款责任。特此证明。";
    NSString *text5 = [NSString stringWithFormat:@"本证明仅用于证明 %@ 在广东宝坤网络小额贷款有限公司的贷款已结清不作其他用途。", name];
    
    
    NSString *finalStr = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@", text1 , text2 , text3 , text4 , text5];
    
    // 创建可变属性字符串
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:finalStr];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 10;  // 设置行间距为10，调整每行之间的垂直间距
    paragraphStyle.paragraphSpacing = 10;
    
    NSRange range1 = [finalStr rangeOfString:name];
    NSRange range2 = [finalStr rangeOfString:idCardNumber];
    NSRange range3 = [finalStr rangeOfString:[NSString stringWithFormat:@"%@ 向广东",applyTime]];
    NSRange range4 = [finalStr rangeOfString:contractNumber];
    NSRange range5 = [finalStr rangeOfString:amount];
    NSRange range6 = [finalStr rangeOfString:amountCN];
    NSRange range7 = [finalStr rangeOfString:[NSString stringWithFormat:@"%@ 至",startTime]];
    NSRange range8 = [finalStr rangeOfString:[NSString stringWithFormat:@"%@ 。",endTime]];
    NSRange range9 = [finalStr rangeOfString:[NSString stringWithFormat:@"%@ ，",lastRepayTime]];
    NSRange range10 = [finalStr rangeOfString:[NSString stringWithFormat:@"%@ 在广东",name]];
    
    
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:range1];
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:range2];
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:NSMakeRange(range3.location, applyTime.length)];
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:range4];
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:range5];
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:range6];
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:NSMakeRange(range7.location, startTime.length)];
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:NSMakeRange(range8.location, endTime.length)];
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:NSMakeRange(range9.location, lastRepayTime.length)];
    [self handleStringInAttributedString:attr paragraphStyle:paragraphStyle finalStr:finalStr range:NSMakeRange(range10.location, name.length)];
    
    self.contentLab.attributedText = attr;
    
    // 计算文本的高度
//    CGSize maxSize = CGSizeMake(SCREEN_WIDTH - 32.f - 48.f, CGFLOAT_MAX);
//    CGRect textRect = [attr boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
}

+ (NSString *)getFinalString:(FPSettleModel *)model {
    
    // 申请时间
    NSString *userApplyTime = [FPLoanDetailModel  timeOfParam:model.createTime];
    // 放款日期
    NSString *lendersTime = [FPLoanDetailModel  timeOfParam:model.lendersTime];
    // 审核通过日期
    NSString *auditPassageTime = [FPLoanDetailModel  timeOfParam:model.auditPassageTime];
    // 最后还款日期 最后一期的规定日期
    NSString *lastRepaymentDate = [FPLoanDetailModel  timeOfParam:model.lastRepaymentDate];
    
    NSString *name              = [NSString stringWithFormat:@"【 %@ 】" , FPUserInfoModel.sharedUserInfo.idcardName];
    NSString *idCardNumber      = [NSString stringWithFormat:@"【 %@ 】" , FPUserInfoModel.sharedUserInfo.idcardNumber];
    NSString *applyTime         = [NSString stringWithFormat:@"【 %@ 】" , userApplyTime];
    NSString *contractNumber    = [NSString stringWithFormat:@"【 %@ 】" , model.contractNumber]; // 合同号
    NSString *amount            = [NSString stringWithFormat:@"【 小写：%@ 元" , model.amount];
    NSString *amountCN          = [NSString stringWithFormat:@" 大写：%@ 】" , [NSString toCapitalLetters:model.amount]];
    NSString *startTime         = [NSString stringWithFormat:@"【 %@ 】" , lendersTime];
    NSString *endTime           = [NSString stringWithFormat:@"【 %@ 】" , lastRepaymentDate];
    NSString *lastRepayTime     = [NSString stringWithFormat:@"【 %@ 】" , lastRepaymentDate]; // 截止日期
    
    NSString *text1 = [NSString stringWithFormat:@" %@ ，身份证号为：", name];
    NSString *text2 = [NSString stringWithFormat:@" %@ 于 %@ 向广东宝坤网络小额贷款有限公司申请了一笔贷款，贷款合同编号为 %@ ，贷款金额为人民币 %@，%@ ，贷款期限自 %@ 至 %@ 。" , idCardNumber , applyTime , contractNumber , amount , amountCN , startTime , endTime];
    NSString *text3 = [NSString stringWithFormat:@"截至 %@ ，借款人已按照贷款合同约定，足额偿还了该笔贷款的全部本金、利息及相关费用，贷款已全部结清。", lastRepayTime];
    NSString *text4 = @"经核实，上述贷款已全额结清，借款人不再对该笔贷款承担任何还款责任。特此证明。";
    NSString *text5 = [NSString stringWithFormat:@"本证明仅用于证明 %@ 在广东宝坤网络小额贷款有限公司的贷款已结清不作其他用途。", name];
    
    
    NSString *finalStr = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@", text1 , text2 , text3 , text4 , text5];
    
    return finalStr;
}

- (void)handleStringInAttributedString:(NSMutableAttributedString *)attr paragraphStyle:(NSMutableParagraphStyle *)paragraphStyle finalStr:(NSString *)finalStr range:(NSRange)range {
    
    [attr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, finalStr.length)];
    [attr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range];
    [NSString stringToAttribute:attr color:WeightColor range:range];
}



@end
